﻿using AduSkin.Controls.Metro;
using GrabFieldr.Models.Impls;
using GrabFieldr.Models.Interface;

//using AduSkin.Demo.UserControls;
using Prism.Ioc;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;

namespace NewDeMo.ViewModels
{
    public class HomePageViewModel : BindableBase, INavigationAware
    {
        // 示例绑定属性
        private int _receivedData;
        public ICommand Btn_Jump { get; private set; }

        public ICommand Btn_Subscribe { get; private set; }

        public ICommand Btn_Snipe { get; private set; }
        public int ReceivedData
        {
            get { return _receivedData; }
            set { SetProperty(ref _receivedData, value); }
        }
        //资源锁
        private readonly object _lockObject = new object();

        private IRegionManager regionManger;
        private IMachineModel machineModel;
        //运动日期按钮列表
        public List<AduRadioButton> SportDateBtnList = new List<AduRadioButton>();
        ////运动时间标签列表
        //public List<AduFormLable> TimeFormLableList= new List<AduFormLable>();
        ////运动字段标签列表
        //public List<AduFormLable> FieldFormLableList = new List<AduFormLable>();
        //运动时间输入框列表
        AduFormLable TimeFormLable1 = new AduFormLable();
        //场地字段输入框列表
        AduFormLable FieldFormLable = new AduFormLable();
        //选中时间
        private DateTime _selectedDateTime;
        //定时器
        private System.Timers.Timer DateNumTimer { get; set; }
        public DateTime SelectedDateTime
        {
            get { return _selectedDateTime; }
            set
            {
                if (SetProperty(ref _selectedDateTime, value))
                {
                    // 属性值发生变化后，执行一些操作
                    OnDateTimeChanged();
                }
            }
        }


        public HomePageViewModel(IMachineModel machineModel,IRegionManager region)
        {
            ReceivedData = 0; // 初始化属性
            Btn_Jump = new DelegateCommand<object>(Btn_Jump_Click);
            // 假设 Btn_Subscribe_Click 是 async Task 类型
            Btn_Subscribe = new DelegateCommand(async () => await Btn_Subscribe_Click());
            Btn_Snipe = new DelegateCommand(async () => await Btn_Snipe_Click());
            regionManger = region;
            this.machineModel = machineModel;
            //Btn_Jump = AddTaskCommand(() => KafkaReport());

            // 初始化时间段和场地列表
            //TimeSlots = new ObservableCollection<string> { "10:00-12:00", "14:00-16:00", "16:30-18:30", "19:30-21:30" };
            TimeSlots = new ObservableCollection<string> { "17:30-19:30", "19.30-21:30" };

            Fields = new ObservableCollection<string> { "1号场", "2号场" };

            // 初始化命令
            SelectionChangedCommand = new DelegateCommand<object>(OnSelectionChanged);

            TimerInit();

        }

        private string _selectedTime;
        public string SelectedTime
        {
            get { return _selectedTime; }
            set { SetProperty(ref _selectedTime, value); }
        }

        private string _selectedField;
        public string SelectedField
        {
            get { return _selectedField; }
            set { SetProperty(ref _selectedField, value); }
        }

        private int _selectedTimeIndex;
        public int SelectedTimeIndex
        {
            get { return _selectedTimeIndex; }
            set { SetProperty(ref _selectedTimeIndex, value); }
        }

        // 用于绑定的列表
        public ObservableCollection<string> TimeSlots { get; set; }
        public ObservableCollection<string> Fields { get; set; }

        // 处理选择变化的命令
        //public DelegateCommand<object> SelectionChangedCommand { get; private set; }

        public DelegateCommand<object> SelectionChangedCommand { get; private set; }

        private void OnSelectionChanged(object parameter)
        {
            // parameter 可以用来传递额外的信息，比如是哪个ComboBox触发的
            // 但这里我们直接使用绑定属性 SelectedTime, SelectedField, SelectedTimeIndex
            // 这些属性已经在用户选择时通过绑定自动更新了

            // 在这里处理选择变化后的逻辑，例如：
            // System.Diagnostics.Debug.WriteLine($"时间: {SelectedTime}, 索引: {SelectedTimeIndex}, 场地: {SelectedField}");
            // 也可以调用 model 的方法
            // machineModel.UpdateSelection(SelectedTimeIndex, SelectedField);
        }

        public void Btn_Jump_Click(object obj)
        {
            //Message = "我是ExtrPlat页面,点击了按钮";
            //var p = new NavigationParameters();
            //p.Add("data", 200);
            regionManger.RequestNavigate("ContentRegion", "ExtrPlat");
        }
        /// <summary>
        /// 订阅按键
        /// </summary>
        /// <returns></returns>
        public async Task Btn_Subscribe_Click()
        {
            //Message = "我是ExtrPlat页面,点击了按钮";
            //var p = new NavigationParameters();
            //p.Add("data", 200);
            //machineModel._FieldGet.Test();
            //machineModel._FieldGet.GetFieldTest();

            machineModel.BookTime = SelectedDateTime;
            machineModel.IsBooked = true;
            await machineModel._FieldGet.ReadyGetField();
            //await machineModel._FieldGet.GetField();
        }

        /// <summary>
        /// 狙击按键
        /// </summary>
        /// <returns></returns>
        public async Task Btn_Snipe_Click()
        {
            //Message = "我是ExtrPlat页面,点击了按钮";
            //var p = new NavigationParameters();
            //p.Add("data", 200);
            //machineModel._FieldGet.Test();
            //machineModel._FieldGet.GetFieldTest();

            //machineModel.BookTime = SelectedDateTime;
            //machineModel.IsBooked = true;
            //await machineModel._FieldGet.ReadyGetField();
            await machineModel._FieldGet.GetField();
        }

        //时间改变操作
        private void OnDateTimeChanged()
        {
            // 处理日期时间变化的逻辑
            
        }

        /// <summary>
        /// 定时器初始化
        /// </summary>
        private void TimerInit()
        {
            // 绑定异步事件处理方法（注意使用async void）
            DateNumTimer = new System.Timers.Timer();
            DateNumTimer.Elapsed += async (sender, e) => await OnTimedEventAsync();

            DateNumTimer.AutoReset = true; // 循环触发
            DateNumTimer.Enabled = true;   // 启动定时器
        }

        /// <summary>
        /// 异步任务方法（返回Task）
        /// </summary>
        private async Task OnTimedEventAsync()
        {
            // 获取当前时间并格式化为字符串（包含毫秒）
            lock (_lockObject)
            {
                DateTime currentTime = DateTime.Now;
                if (currentTime > machineModel.BookTime && machineModel.IsBooked)
                {
                    machineModel.IsBooked = false;
                    machineModel._FieldGet.GetField();
                }
            }
            //FormattedTime = currentTime.ToString("HH:mm:ss"); // 24小时制，精确到毫秒

            //WeekIndex = (int)DateTime.Now.DayOfWeek; // 获取索引 (0 是星期日)
        }
        bool IRegionAware.IsNavigationTarget(NavigationContext navigationContext)
        {
            return true; // 默认重用当前实例
        }

        void IRegionAware.OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }

        void IRegionAware.OnNavigatedTo(NavigationContext navigationContext)
        {
            navigationContext.Parameters.GetValue<int>("data");
        }


    }
}
